/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.Phong_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class Phong_DAO {
    
    public static String Get_MaPhong() throws SQLException{
        String MaPH = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "PH");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaPH = Rs.getString(1);
        }
        return MaPH;
    }
    
    public static ArrayList<Phong_DTO> GetALL()
    {
        ArrayList<Phong_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from Phong" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                Phong_DTO Phong = new Phong_DTO();
                Phong.setMaPhong(rs.getString("MaPhong"));
                Phong.setTrangThai(rs.getInt("TrangThai"));
                Phong.setMaToa(rs.getString("MaToa"));
                ds.add(Phong);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(Phong_DTO Phong)
    {
        boolean flag = false;
        try {
            String sql = "Insert Phuong Values('"+Phong.getMaPhong()+"','"+Phong.getTrangThai()+"','"+ Phong.getMaToa() +"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(Phong_DTO Phong)
    {
        boolean flag = false;
        try {
            String sql = "Update Phong Set TrangThai = "+ Phong.getTrangThai() +", MaToa = '"+Phong.getMaToa()+"' where MaPhong = '"+ Phong.getMaPhong()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maPhong)
    {
        boolean flag = false;
        try {
            String sql = "Delete from Phong where MaPhong = '"+maPhong+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
}
